#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
#endregion

namespace ProjectNocturnalFool
{
    /// <summary>
    /// Implements visuals 
    /// #TODO move logic to GameBoardLogic
    /// </summary>
    class GameboardControl : UserControl
    {
        #region Fields

        TileDrawer tileDrawer;
        EntityDrawer entityDrawer;

        Point mLoc;
        Point realPoint;
        //Point visualPoint;
        
        //HACK-vlad
        //SpriteAnimation vlad;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public GameboardControl()
            : base()
        {
        }


        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {

            TileImage.imageSheet = TacticsGame.Instance.Content.Load<Texture2D>("GameBoardTiles/testImage");

            tileDrawer = new TileDrawer(TacticsGame.Instance.Content.Load<Texture2D>("GameBoardMisc/mouseMap"), TacticsGame.Instance.Content.Load<Texture2D>("GameBoardMisc/mouseHighlight"));
            entityDrawer = new EntityDrawer();

            InputMgr.Instance.getMouse(out mLoc.X, out mLoc.Y);
            //initialize the camera
            Camera.Init();
            Camera.viewWidth = VirtualScreen.Instance.VirtualWidth;//this.graphics.PreferredBackBufferWidth;
            Camera.viewHeight = VirtualScreen.Instance.VirtualHeight;// this.graphics.PreferredBackBufferHeight;
            Camera.worldWidth = (int)((GameLogic.Instance.gameBoardLogic.tiles.tileSetWidth+0.5) * TileImage.tileImageStepX);
            width = Camera.worldWidth;
            Camera.worldHeight = (int)((GameLogic.Instance.gameBoardLogic.tiles.tileSetHeight+1) * TileImage.tileImageStepY);
            height = Camera.worldHeight;
            


            //Thread.Sleep(1000);
            //// once the load has finished
            //ScreenManager.Game.ResetElapsedTime();
        }
        
        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            //#HACK
            //vlad.CurrentAnimation = "AdamWest";
            //vlad.Update(gameTime);
            
            InputMgr.Instance.getMouse(out mLoc.X, out mLoc.Y);
            if (Hover)
            {
                scroll();
                input();
                
                //DebugMgr.Instance.addDebug("viewAbsBounds:" + (Camera.location.X).ToString() + "-" + (Camera.location.X+Camera.viewWidth).ToString() + ", " + (Camera.location.Y).ToString() + "-" + (Camera.location.Y+Camera.viewHeight).ToString().ToString(), 4);
                //DebugMgr.Instance.addDebug("worldWidth,worldHeight: " + Camera.worldWidth + ", " + Camera.worldHeight, 5);
                width = 9999;
                height = 9999;

                Camera.Update();
            }


            entityDrawer.Update(gameTime);
            //GameLogic.Instance.gameBoardLogic.Update(gameTime);


            //DebugMgr.Instance.addDebug("Total Mouse X,Y - " + (mLoc.X + Camera.Location.X).ToString() + ", " + (mLoc.Y + Camera.Location.Y).ToString(), 3);
            //DebugMgr.Instance.addDebug("BoardX,Y - " + (Camera.location.X).ToString() + ", " + (Camera.location.Y).ToString(), 4);
            //DebugMgr.Instance.addDebug("Width, Height - " + (width).ToString() + ", " + (height).ToString(), 5);
            //DebugMgr.Instance.addDebug("SCALE - " + Camera.scale.ToString(), 6);
            
            //DebugMgr.Instance.addDebug("Centre - " + Camera.getCentre(), 6);

            
            
        }

        public void input() {
            if (InputMgr.Instance.IsButtonPress(new PlayerIndex(), Input.One))
            {
                Camera.zoomIn();

            }
            if (InputMgr.Instance.IsButtonPress(new PlayerIndex(), Input.Two))
            {
                Camera.zoomOut();
            }

   


        }

        public void scroll() {
            if (mLoc.X < 20)// && (int)Camera.Location.X>0)
            {
                Camera.Move(new Vector2(-2,0));
            }
            else if (mLoc.X > Camera.viewWidth - 20)// && (int)Camera.Location.X + Camera.viewWidth - TileImage.tileImageStepX/2 < Camera.worldWidth)
            {
                Camera.Move(new Vector2(2, 0));
            }

            if (mLoc.Y < 20)// && (int)Camera.Location.Y > 0)
            {
                Camera.Move(new Vector2(0, -2));
            }
            else if (mLoc.Y > Camera.viewHeight - 20)// && (int)Camera.Location.Y + Camera.viewHeight - TileImage.tileImageStepY/2 < Camera.worldHeight)
            {
                Camera.Move(new Vector2(0, 2));
            }
        }


        /// <summary>
        /// Draws the gameplay screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            DebugMgr.Instance.addDebug(mLoc.ToString(), 2);
            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            tileDrawer.Draw(gameTime);
            entityDrawer.Draw(gameTime);

            hilightDraw();
           
            //HACK-vlad
            //vlad.Draw(TacticsGame.Instance.spriteBatch, 0, 0);
            


            TacticsGame.Instance.spriteBatch.End();
                        
        }//end of draw method


        //draws the highlight/hilight
        public void hilightDraw() {

            if (Hover)
            {
                GameLogic.Instance.gameBoardLogic.tiles.deselect();


                Vector2 highlightLoc = Camera.ScreenToWorld(new Vector2(mLoc.X, mLoc.Y));
                
                Point highlightPoint = tileDrawer.WorldToTile(new Point((int)(highlightLoc.X/Camera.scale), (int)(highlightLoc.Y/Camera.scale)));

                realPoint.Y = GameLogic.Instance.gameBoardLogic.tiles.getRealTileRow(highlightPoint.X, highlightPoint.Y);
                realPoint.X = highlightPoint.X;

                Point tempPt;
                tempPt = entityDrawer.checkSpritesForMouseHover(highlightLoc);
                if (tempPt.X >= 0)
                {
                    realPoint = tempPt;
                }

                highlightPoint = realPoint;
                if (highlightPoint.X >= 0 && highlightPoint.Y >= 0 && 
                    highlightPoint.X < GameLogic.Instance.gameBoardLogic.tiles.tileSetWidth &&
                    highlightPoint.Y < GameLogic.Instance.gameBoardLogic.tiles.tileSetHeight)
                {
                    GameLogic.Instance.gameBoardLogic.tiles.rows[highlightPoint.Y].columns[highlightPoint.X].mouseSelected = true;
                }

                
                
                /*
                
                visualPoint.Y= realPoint.Y - 2 * GameLogic.Instance.gameBoardLogic.tiles.getAltitude(realPoint.X, realPoint.Y);
                visualPoint.X = realPoint.X;
                Point newVisual;
                newVisual = GameLogic.Instance.gameBoardLogic.tiles.getUpR(visualPoint, 0);
                visualPoint = newVisual;
                 */
                //////////////

                //old hilight stuff below

                //int realHilightRowOffset = 0;
                //int visualHilightRowOffset = 0;
                


                //if(Math.Abs((visualPoint.Y)) % 2 == 1)
                //{
                //    visualHilightRowOffset = TileImage.oddRowXOffset;
                //}
                //if(Math.Abs((realPoint.Y)) % 2 == 1)
                //{
                //    realHilightRowOffset = TileImage.oddRowXOffset;
                //}

                // TacticsGame.Instance.spriteBatch.Draw(
                //                mouseHilight,
                //                Camera.WorldToScreen(
                //                    new Vector2(
                //                        (visualPoint.X * TileImage.tileImageStepX) + visualHilightRowOffset,
                //                       (visualPoint.Y + 0) * TileImage.tileImageStepY)),
                //                new Rectangle(0, 0, 64, 32),
                //                Color.Blue * 0.2f,
                //                0.0f,
                //                Vector2.Zero,
                //                Camera.scale,
                //                SpriteEffects.None,
                //                0.0f);
                ////HACK testHighlight
                //TacticsGame.Instance.spriteBatch.Draw(
                //                testHilight,
                //                Camera.WorldToScreen(
                //                    new Vector2(
                //                        (realPoint.X * TileImage.tileImageStepX) + realHilightRowOffset,
                //                        (realPoint.Y + 0) * TileImage.tileImageStepY)),
                //                new Rectangle(0, 0, 64, 32),
                //                Color.Blue * 0.2f,
                //                0.0f,
                //                Vector2.Zero,
                //                Camera.scale,
                //                SpriteEffects.None,
                //                0.0f);
            }
        }


        #endregion
    }
}
